﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.ContractFollowUp
{
    public partial class MaintainBillApproveList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["customername"] != null)
                    txtContractCodeBasic.Text = Request.QueryString["customername"].ToString();
                if (Request.QueryString["instrumentname"] != null)
                    txtinstrumentName.Text = Request.QueryString["instrumentname"].ToString();
                //TaskType.BindEnum(EnumeratorHelper.GetFieldTexts(typeof(Enumerator.SalesService)));
                //TaskType.Items.Add(new ListItem("--不限--", "0"));
                //TaskType.SelectedValue = "0";
                BindList(1);
            }
        }

        private void BindList(int pageindex)
        {
            string Whereclause = " and TaskType=1 and AccessoryCheckStatus > 0 ";

            if (txtContractCodeBasic.Text.Trim() != "")
            {
                Whereclause += " and CustomerID in ( select CustomerID from vw_CustomerAllInfo where CustomerName like '%" + txtContractCodeBasic.Text.Trim() + "%' or  CustomerArea like '%" + txtContractCodeBasic.Text.Trim() + "%')";
            }

            if (this.txtSerialNum.Text.Trim() != "")
            {
                Whereclause += " and InstrumentSerialNum  like '%" + txtSerialNum.Text.Trim() + "%'";
            }

            if (this.txtinstrumentName.Text.Trim() != "")
            {
                Whereclause += " and InstrumentID in ( select InstrumentID from Cms_Wsm_InstrumentInfo where InstrumentName like '%" + txtinstrumentName.Text.Trim() + "%')";
            }

            //if (TaskType.SelectedValue.ToString() != "0")
            //{
            //    Whereclause += " and TaskType = " + TaskType.SelectedValue.ToString();
            //}

            if (checkStatus.SelectedValue.ToString() == "1")
            {
                Whereclause += " and AccessoryCheckStatus in(1,2)";
            }

            if (checkStatus.SelectedValue.ToString() == "2")
            {
                Whereclause += " and AccessoryCheckStatus = 3";
            }


            if (employeeGUID.Value.Trim() != "")
            {
                Whereclause += " and RegisterID in  (select RegisterID from Cms_Wsm_MaintainBill where Maintainer = '" + employeeGUID.Value.Trim().ToString() + "')";
            }

            int RecordCount = 0;
            DataTable dt = new MaintainBillBll().QueryMaintainBll(Whereclause, "ExpectEndDate asc", AspNetPager1.PageSize, pageindex, out RecordCount);

            RepeaterUsers.DataSource = dt;
            RepeaterUsers.DataBind();
            AspNetPager1.RecordCount = RecordCount;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindList(AspNetPager1.CurrentPageIndex);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindList(1);
        }

        protected void RepeaterUsers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label CustomerID = e.Item.FindControl("CustomerID") as Label;
                Label CustomerName = e.Item.FindControl("CustomerName") as Label;
                Label Maintainer = e.Item.FindControl("Maintainer") as Label;
                Label MaintainerName = e.Item.FindControl("MaintainerName") as Label;

                Label checkStatus = e.Item.FindControl("checkStatus") as Label;
                Label checkStatusName = e.Item.FindControl("checkStatusName") as Label;
                //Label BeginDate = e.Item.FindControl("BeginDate") as Label;
                //Label EndTime = e.Item.FindControl("EndTime") as Label;
                Label CustomerArea = e.Item.FindControl("CustomerArea") as Label;
                Label InstrumentID = e.Item.FindControl("InstrumentID") as Label;
                Label Instrumentname = e.Item.FindControl("Instrumentname") as Label;
                Label RegisterID = e.Item.FindControl("RegisterID") as Label;
                if (CustomerID != null)
                {
                    CtrtCustomerEntity entity = new SystemBll().Get<CtrtCustomerEntity>(new Guid(CustomerID.Text));
                    CustomerName.Text = entity.CustomerName;

                    if (Maintainer.Text.Trim() != "")
                    {
                        SysUserEntity sysuserentity = new SystemBll().GetUserEntityFromCache(new Guid(Maintainer.Text));
                        if (sysuserentity != null)
                            MaintainerName.Text = sysuserentity.UserName;
                    }

                    checkStatusName.Text = (checkStatus.Text != "3" ? (checkStatus.Text != "2" ? "已保存提交" : "组长已复查") : "已审核");

                    WsmCustomerExtensionEntity extensionEntity = new SystemBll().Get<WsmCustomerExtensionEntity>(new Guid(CustomerID.Text));
                    if (extensionEntity != null)
                        CustomerArea.Text = extensionEntity.CustomerArea;

                    WsmInstrumentInfoEntity insurment = new SystemBll().Get<WsmInstrumentInfoEntity>(new Guid(InstrumentID.Text));
                    if (insurment != null)
                        Instrumentname.Text = insurment.instrumentName;

                    //根据审批状态
                }
            }
        }
    }
}
